package com.orange.delivery.presentation.util;

import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.StringResourceModel;

/**
 * Generic model that provides a string indicating the number of rows in a List, using the string available with the "list.counter"
 * key
 * 
 * @author BEAL6226
 * 
 * @param <T>
 *            count the list of <T>
 */
public class CounterModel<T> extends AbstractReadOnlyModel<String> implements IModel<String> {
	private static final long serialVersionUID = 1L;

	private final IModel<Long> sizeModel;

	public CounterModel(IModel<Long> sizeModel) {
		super();
		this.sizeModel = sizeModel;
	}

	@Override
	public String getObject() {
		long size;
		// model object should be null
		if (sizeModel.getObject() == null) {
			size = 0;
		} else {
			size = sizeModel.getObject();
		}

		StringResourceModel model = new StringResourceModel("list.counter", null, new Object[] { size, size > 1 ? "s" : "" });
		return model.getObject();

	}

}
